﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iCollect.login;
using iCollect.CollectDetail;
using System.Reflection;
using iCollect.Navigator;
using iCollect.Events;
using iCollect.Constants;

namespace iCollect.Main
{
    public partial class NewUI : Form
    {
        public NewUI()
        {
            InitializeComponent();
            addCollectDetail();
            
            this.Shown += new System.EventHandler(this.HandleShownEvent);
        }

        void HandleShownEvent(object sender, System.EventArgs a)
        {
            LoginForm loginForm = new LoginForm();
            loginForm.Show();
            this.SendToBack();
            this.Visible = false;

            loginForm.RaiseCustomEvent += HandleLoginEvent;
        }

        void HandleLoginEvent(object sender, CustomEventArgs a)
        {
            if (a.state == LoginForm.EXIT)
            {
                this.Close();
            }
            else
            {
                initTitle();
                initNavigator();
                this.Visible = true;
            }
        }

        void initTitle()
        {
            this.Text = "中国石油设备在线监测及故障诊断系统                                       欢迎炼油部的马云使用系统 ";
        }

        void initNavigator()
        {
            foreach (Control control in menuPanel.Controls)
            {
                if (control is RadioButton) 
                {
                   control.Enabled = false;
                }
            }
            selectDeviceButton.Enabled = true;
        }

        void enableNavigator()
        {
            foreach (Control control in menuPanel.Controls)
            {
                if (control is RadioButton)
                {
                    control.Enabled = true;
                }
            }
        }

        private void quit_Click(object sender, EventArgs e)
        {
            DialogResult MsgBoxResult;//设置对话框的返回值
            MsgBoxResult = MessageBox.Show("您确认要退出系统吗？",//对话框的显示内容
            "提示",//对话框的标题
            MessageBoxButtons.YesNo,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Exclamation,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button2);//定义对话框的按钮式样
            if (MsgBoxResult == DialogResult.Yes)//如果对话框的返回值是YES（按"Y"按钮）
            {
                this.Close();
            }
            if (MsgBoxResult == DialogResult.No)//如果对话框的返回值是NO（按"N"按钮）
            {
                //do nothing;
            }
        }

        private void navigator_select(object sender, System.EventArgs a)
        {
            RadioButton node = (RadioButton)sender;
            if (node == null)
            {
                return;
            }
            string controlName = node.Name;
            
            if (controlName == "deviceInfoButton")
            {
                this.mainPanel.Controls.SetChildIndex(c1, 0);
            }
            else if (controlName == "collectConfigButton")
            {
                this.mainPanel.Controls.SetChildIndex(c2, 0);
            }
            else if (controlName == "13")
            {
                this.mainPanel.Controls.SetChildIndex(c3, 0);
            }
            else if (controlName == "14")
            {
                this.mainPanel.Controls.SetChildIndex(c4, 0);
            }
            //System.Type type = Type.GetType("CollectDetail.Collect1");
            else if (controlName == "21")
            {
                this.mainPanel.Controls.SetChildIndex(c0, 0);
                openFileDialog1.Filter = "数据文件(*.dat)|*.dat|所有文件|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string str = openFileDialog1.FileName;
                    System.Diagnostics.Process.Start(str);
                }
            }
            else if (controlName == "22")
            {
                this.mainPanel.Controls.SetChildIndex(c0, 0);
                saveFileDialog1.Filter = "文本文件|*.txt|WORD文件|*.doc|所有文件|*.*";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string str = saveFileDialog1.FileName;
                    System.Diagnostics.Process.Start(str);
                }
            }
            else if (controlName == "23")
            {
                this.mainPanel.Controls.SetChildIndex(g1, 0);
            }
            else if (controlName == "24")
            {
                this.mainPanel.Controls.SetChildIndex(g2, 0);
            }
            else if (controlName == "mainMonitorButton")
            {
                this.mainPanel.Controls.SetChildIndex(oc1, 0);

                c1.loadDevice();
                c2.loadDevice();
                g1.loadDevice();
                g2.loadDevice();
            }
            else if (controlName == "32")
            {
                this.mainPanel.Controls.SetChildIndex(oc2, 0);
            }
            else if (controlName == "33")
            {
                this.mainPanel.Controls.SetChildIndex(oc3, 0);
            }
            else if (controlName == "historyTrendButton")
            {
                this.mainPanel.Controls.SetChildIndex(q1, 0);
            }
            else if (controlName == "historyAlertButton")
            {
                this.mainPanel.Controls.SetChildIndex(q2, 0);
                //for (int i = 0; i < 8; i++)
                //{
                //    DataGridViewRow row = new DataGridViewRow();
                //    dataGridView6.Rows.Add(row);
                //}
            }
            else if (controlName == "uAdmin")
            {
                this.mainPanel.Controls.SetChildIndex(um, 0);
            }
            else
            {
                this.mainPanel.Controls.SetChildIndex(c0, 0);

            }
             
        }
        private Control c0 = new Control();
        private Collect1 c1 = new Collect1();
        private Collect2 c2 = new Collect2();
        private Collect3 c3 = new Collect3();
        private Collect4 c4 = new Collect4();

        private Config1 g1 = new Config1();
        private Config2 g2 = new Config2();

        private OnlineCollect1 oc1 = new OnlineCollect1();
        private OnlineCollect2 oc2 = new OnlineCollect2();
        private OnlineCollect3 oc3 = new OnlineCollect3();

        private Query1 q1 = new Query1();
        private Query2 q2 = new Query2();

        private UserAdmin um = new UserAdmin();

        private void addCollectDetail()
        {
            addCollectDetail(c0);
            addCollectDetail(c1);
            addCollectDetail(c2);
            addCollectDetail(c3);
            addCollectDetail(c4);

            addCollectDetail(g1);
            addCollectDetail(g2);

            addCollectDetail(oc1);
            addCollectDetail(oc2);
            addCollectDetail(oc3);

            addCollectDetail(q1);
            addCollectDetail(q2);

            addCollectDetail(um);
        }


        private void addCollectDetail(Control ctr)
        {
            ctr.Dock = System.Windows.Forms.DockStyle.Fill;
            mainPanel.Controls.Add(ctr);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timeLabel.Text = DateTime.Now.ToString();
        }

        private void selectDeviceButtonClick(object sender, EventArgs e)
        {
        }

        void HandleDeviceEvent(object sender, CustomEventArgs a)
        {
            if (a.state == LoginForm.EXIT)
            {
                //t4u.Enabled = false;
            }
            else
            {
                GlobalConstants.initChannelMap();
                enableNavigator();
                c1.loadDevice();
                c2.loadDevice();
                g1.loadDevice();
                g2.loadDevice();
                oc1.loadDevice();
                q1.loadDevice();
                q2.loadDevice();
            }
            
        }

        private void selectDeviceButton_CheckedChanged(object sender, EventArgs e)
        {
            if (selectDeviceButton.Checked)
            {
                Form2 frm2 = Form2.getInstance();
                frm2.Show();
                frm2.RaiseCustomEvent += HandleDeviceEvent;
            }
        }
    }
}
